#pragma once
#include <mutex>
#include <map>
#include <memory>
#include "../../../src/log/LogFactory.h"

namespace mgr
{
	using namespace std;
	using namespace LibTools;

	template<class T>
	class SessionMgr
	{
	public:
		static void DeleteSession(T* s)
		{
			lock_guard<mutex> lk(_mutexSession);
			_sessions.erase(s);
			LOG_TOOLS(LOG_NORMAL) << "DeleteSession:" << s << ", size = " << _sessions.size();
		}

		static void Insert(shared_ptr<T> s)
		{
			lock_guard<mutex> lk(_mutexSession);
			_sessions.insert({ s.get(), s });
			LOG_TOOLS(LOG_NORMAL) << "Insert:" << s.get() << ", size = " << _sessions.size();
		}

		static void DeleteFront()
		{
			lock_guard<mutex> lk(_mutexSession);
			shared_ptr<T> s = _sessions.begin()->second;
			_sessions.erase(_sessions.begin());
			LOG_TOOLS(LOG_NORMAL) << "DeleteSession:" << s.get() << ", size = " << _sessions.size();
		}

		static bool Empty()
		{
			lock_guard<mutex> lk(_mutexSession);
			return _sessions.empty();
		}
	private:
		static mutex _mutexSession;
		static map<T*, shared_ptr<T>> _sessions;
	};

	template<class T>
	mutex SessionMgr<T>::_mutexSession;

	template<class T>
	map<T*, shared_ptr<T>> SessionMgr<T>::_sessions;
}